﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Devonline.Core;

namespace Devonline.Entity
{
    /// <summary>
    /// 字符串类型的默认基类
    /// </summary>
    public abstract class EntitySetWithCreateAndUpdate : EntitySetWithCreateAndUpdate<string>
    {
    }

    /// <summary>
    /// 带更新的基础数据对象模型抽象基类
    /// </summary>
    public abstract class EntitySetWithCreateAndUpdate<TKey> : EntitySetWithCreate<TKey>, IEntitySetWithCreateAndUpdate<TKey> where TKey : IConvertible
    {
        /// <summary>
        /// 更新时间
        /// </summary>
        [Column("updated_on"), DisplayName("更新时间"), Excel]
        public virtual DateTime? UpdatedOn { get; set; }
        /// <summary>
        /// 更新人
        /// </summary>
        [Column("updated_by"), DisplayName("更新人"), MaxLength(36), Excel]
        public virtual string UpdatedBy { get; set; }
        /// <summary>
        /// 备注说明
        /// </summary>
        [Column("description"), DisplayName("备注说明"), MaxLength(256), Excel]
        public virtual string Description { get; set; }

        /// <summary>
        /// 更新方法
        /// </summary>
        public virtual void Update(string updatedBy = null)
        {
            RowVersion = KeyGenerator.GetKey<TKey>();
            UpdatedOn ??= DateTime.UtcNow;
            UpdatedBy ??= updatedBy ?? AppSettings.USER_SYSTEM;
        }
    }
}
